package com.example.combwx.mvp.view.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.example.combwx.R;
import com.example.combwx.db.UserUtility;
import com.example.combwx.mvp.base.model.BaseEntity;
import com.example.combwx.mvp.base.view.BaseFragment;
import com.example.combwx.mvp.contract.LREContract;
import com.example.combwx.mvp.di.DaggerLREComponent;
import com.example.combwx.mvp.di.LREModules;
import com.example.combwx.mvp.model.HttpCode;
import com.example.combwx.mvp.model.entity.FollowEntity;
import com.example.combwx.mvp.model.entity.UserEntity;
import com.example.combwx.mvp.model.entity.UserFollowEntity;
import com.example.combwx.mvp.presenter.LREPresenter;
import com.example.combwx.mvp.view.people.ConsumerActivity;
import com.example.combwx.mvp.view.people.adapter.FollowNewsAdapter;
import com.example.combwx.mvp.view.people.adapter.UserFollowAdapter;
import com.example.combwx.utils.ToasetUtils;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FollowFragment extends BaseFragment<LREPresenter> implements
        LREContract.ILREViwe, OnRefreshLoadMoreListener,
        OnItemChildClickListener {
    private LinearLayout followInil;
    private SmartRefreshLayout followSmart;
    private RecyclerView followNewsRv;
    private View headView;
    private RecyclerView follow_author_rv;
    private LinearLayoutManager author,news;
    private FollowNewsAdapter followNewsAdapter;
    private UserFollowAdapter userFollowAdapter;

//    @Override
//    public View addStatusView() {
//        return f(R.id.peop_linn);
//    }
//
//    @Override
//    public int addColor() {
//        return Color.WHITE;
//    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_follow;
    }

    @Override
    public void initView() {

        followSmart = (SmartRefreshLayout) f(R.id.follow_smart);
        followNewsRv = (RecyclerView) f(R.id.follow_news_rv);
        headView = LayoutInflater.from(getContext()).inflate(R.layout.view_follow_head,null);
        follow_author_rv = headView.findViewById(R.id.follow_author_rv);
        followSmart.setEnableLoadMore(true);
        followSmart.setEnableRefresh(true);
        followSmart.setRefreshHeader(new ClassicsHeader(getContext()));
        followSmart.setRefreshFooter(new ClassicsFooter(getContext()));
        followSmart.setOnRefreshLoadMoreListener(this);
        news = new LinearLayoutManager(getContext());
        news.setOrientation(LinearLayoutManager.VERTICAL);
        followNewsRv.setLayoutManager(news);
        author = new LinearLayoutManager(getContext());
        author.setOrientation(LinearLayoutManager.HORIZONTAL);
        follow_author_rv.setLayoutManager(author);
        userFollowAdapter = new UserFollowAdapter();
        followNewsAdapter = new FollowNewsAdapter();
        followNewsAdapter.addHeaderView(headView);
        followNewsRv.setAdapter(followNewsAdapter);
        follow_author_rv.setAdapter(userFollowAdapter);
        userFollowAdapter.setOnItemChildClickListener(this::onItemChildClick);
    }

    @Override
    public void initData() {
        try {
            UserEntity entity = UserUtility.getInstance().getDaoSession().queryBuilder(UserEntity.class).list().get(0);
            Map<String,Object>newsMap = new HashMap<>();
            newsMap.put("code", HttpCode.FOLLOWNEWSCODE);
            newsMap.put("user_id",1);
            Map<String,Object>followMap = new HashMap<>();
            followMap.put("code",HttpCode.USERFOLLOWCODE);
            followMap.put("user_id",1);
            p.requestAll(followMap,newsMap);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void inject() {
        DaggerLREComponent.builder().lREModules(new LREModules(this))
                .build().injectFollowFragment(this);
    }

    @Override
    public void refreshAll(BaseEntity entity) {
        if (entity instanceof FollowEntity){
            FollowEntity followEntity = (FollowEntity) entity;
            followNewsAdapter.setNewInstance(followEntity.getValues());
        }else {
            UserFollowEntity userFollowEntity = (UserFollowEntity) entity;
            userFollowAdapter.setNewInstance(userFollowEntity.getValues());
        }
    }

    @Override
    public void refreshRecyckerView(BaseEntity entity) {
        FollowEntity followEntity = (FollowEntity) entity;
        if (followEntity.getValues() == null){
            ToasetUtils.showMsg("刷新失败");
            return;
        }

        followNewsAdapter.setNewInstance(followEntity.getValues());
    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {
        FollowEntity followEntity = (FollowEntity) entity;
        if (followEntity.getValues() == null){
            ToasetUtils.showMsg("最后一页");
            return;
        }
        followNewsAdapter.addData(followEntity.getValues());
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore();

        try {
            UserEntity entity = UserUtility.getInstance().getDaoSession().queryBuilder(UserEntity.class).list().get(0);
            Map<String,Object> newsMap = new HashMap<>();
            newsMap.put("code",HttpCode.FOLLOWNEWSCODE);
            newsMap.put("user_id",1);
            p.requestLoad(newsMap);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();

        try {
            UserEntity entity = UserUtility.getInstance().getDaoSession().queryBuilder(UserEntity.class).list().get(0);
            Map<String,Object>newsMap = new HashMap<>();
            newsMap.put("code",HttpCode.FOLLOWNEWSCODE);
            newsMap.put("user_id",1);
            p.requestRefresh(newsMap);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter,
                                 @NonNull View view, int position) {
        ImageView viewById = view.findViewById(R.id.follow_item_img);
        List<UserFollowEntity.Values> data = userFollowAdapter.getData();
        String author_img = data.get(0).author_img;
        String author_name = data.get(0).author_name;
        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putString("text",author_name);
                bundle.putString("img",author_img);
                startPage(bundle, ConsumerActivity.class);
            }
        });


    }
}
